import matplotlib.pyplot as plt
import numpy as np

colors = ['red', 'blue', 'green', 'yellow']
tick_label = ['P', 'R', "F1"]


# 图表绘制类
class GraphUtil:
    def show(self):
        plt.show(block=True)

    def setData(self, datas, xlabel, ylabel, names):
        pass

    def save(self, path):
        plt.savefig(path)

    def setTitle(self, title):
        plt.title(title)


class ScatterGraphUtil(GraphUtil):
    def setData(self, datas, xlabel, ylabel, names):
        plt.clf()
        plt.ylim(0, 1)
        plt.xlabel(xlabel)
        plt.ylabel(ylabel)
        x = np.arange(len(datas[0]))
        plt.xticks([])
        index = 0
        for data in datas:
            plt.scatter(x, data, c=colors[index], label=names[index], alpha=0.5)
            index += 1
        plt.legend(loc='lower left')


class BarGraphUtil(GraphUtil):
    bar_width = 0.25

    def __init__(self, bar_width=0.25):
        self.bar_width = bar_width

    def setData(self, datas, xlabel, ylabel, names):
        plt.clf()
        x = np.arange(len(tick_label))
        plt.xlabel(xlabel)
        plt.ylabel(ylabel)
        plt.ylim(0, 1)
        index = 0
        for data in datas:
            bar = plt.bar(x + self.bar_width * index, data, self.bar_width, align='center', color=colors[index],
                          label=names[index])
            plt.bar_label(bar,fmt="{:.4f}")
            index += 1
        plt.xticks(x + self.bar_width / 2, tick_label)
        plt.legend(loc='lower left')
